<?php

session_start();
$result = get_last_question();
$row = mysqli_fetch_assoc ($result);
$question_id = $row["question_id"];

if(isset($_GET['voted']) || isset($_SESSION['voted'])){
	echo "<span class='heading3'>";
	print($row["question"]);
	echo "</span>";

	if(isset($_SESSION['voted']) || isset($_COOKIE['FitnessMania']))
	{
	}else{
		post_vote($question_id, $_POST['answer']);
	}

	$_SESSION['voted'] = true;
	setcookie("FitnessMania[voted]", "true", time()+ 60*60*24*7);

	$result = get_votes_for_question($question_id);
	$row = mysqli_fetch_assoc( $result );
	$total_votes = $row['votes'];

	$result = get_answers_to_questions($question_id);

	print('<table border="0" cellspacing="3" cellpadding="1">');
	for( $i = 0; $i < mysqli_num_rows( $result ); $i++ )
	{
		$answer = mysqli_fetch_assoc( $result );
		$persentage = $answer['votes']/$total_votes*100;
		$english_format_number = number_format($persentage, 2, '.', '');

		print('<tr>
	            <td>'.$answer['answer'].'</td>

	            <td><img src="/images/voting/blue.gif" height="15" width="'.$persentage.'" alt="" border="0"></td>
	            <td>&nbsp;'.$english_format_number.' %</td>
	          </tr>');

	}
	print('</table>');


}
else{
	echo "<span class='heading4'>";
	print($row["question"]);
	echo "</span>";

	$queryParameters;
	foreach($_GET as $var => $value){
		$queryParameters .= "&".$var."=".$value;
	}
//	echo $queryParameters;
	echo '<form name="vote" action="'.$_SERVER['PHP_SELF'].'?voted=true'.$queryParameters.'" method="post">';

	$result = get_answers_to_questions($question_id);

	printf('<input type="hidden" name="question_id" value="%s"/>', $question_id);

	for( $i = 0; $i < mysqli_num_rows( $result ); $i++ )
	{
		$answer = mysqli_fetch_assoc( $result );
		printf('<input type="radio" value="%s"  name="answer"  /><span onclick="document.vote.answer[%s].checked=true;" >%s</span><br />', $answer['answer_id'], $i, $answer['answer']);
	}

	echo '<input type="submit" name="submit_vote" value="Vote"></form>';
}
?>

